# Replication files for:
# "Generalizability of Heterogeneous Treatment Effect Estimates Across Samples"
# Alexander Coppock, Thomas J. Leeper, and Kevin J. Mullinix
# Proceedings of the National Academy of Sciences, Forthcoming

rm(list = ls())
library(dplyr)
library(readr)

# AEC studies -------------------------------------------------------------

superordinate_id_stacked <- read_rds("superordinate_id_stacked.rds")
superordinate_id <-
  superordinate_id_stacked %>%
  filter(!is.na(Y_s),!is.na(Z),!is.na(Z_particularism)) %>%
  summarize(
    mt_n = sum(sample == "mt"),
    gfk_n = sum(sample == "gfk"),
    original_n = sum(sample == "original")
  )

patriot_act_stacked <- read_rds("patriot_act_stacked.rds")
patriot_act <-
  patriot_act_stacked %>%
  filter(!is.na(Y_s),!is.na(Z)) %>%
  summarize(
    mt_n = sum(sample == "mt"),
    gfk_n = sum(sample == "gfk"),
    original_n = sum(sample == "original")
  )

elite_endorsements_stacked <- read_rds("elite_endorsements_stacked.rds")
elite_endorsements <-
  elite_endorsements_stacked %>%
  filter(Z_imm_match != "control") %>%
  filter(!is.na(Y_s),!is.na(Z)) %>%
  summarize(
    mt_n = sum(sample == "mt"),
    gfk_n = sum(sample == "gfk"),
    original_n = sum(sample == "original")
  )

free_trade_stacked <- read_rds("free_trade_stacked.rds")
free_trade <-
  free_trade_stacked %>%
  filter(sample != "gfk") %>%
  filter(!is.na(Y_s),!is.na(Z),!is.na(Z_Hiscox_valence)) %>%
  summarize(
    mt_n = sum(sample == "mt"),
    gfk_n = sum(sample == "gfk"),
    original_n = sum(sample == "original")
  )

frame_breadth_stacked <- read_rds("frame_breadth_stacked.rds")
frame_breadth <-
  frame_breadth_stacked %>%
  filter(sample != "gfk") %>%
  filter(!is.na(Y_s),!is.na(Z)) %>%
  summarize(
    mt_n = sum(sample == "mt"),
    gfk_n = sum(sample == "gfk"),
    original_n = sum(sample == "original")
  )

polarization_stacked <- read_rds("polarization_stacked.rds")
polarization <-
  polarization_stacked %>%
  filter(sample != "gfk") %>%
  filter(Z_Levendusky != "placebo") %>%
  filter(!is.na(Y_s),!is.na(Z)) %>%
  summarize(
    mt_n = sum(sample == "mt"),
    gfk_n = sum(sample == "gfk"),
    original_n = sum(sample == "original")
  )

immigration_stacked <- read_rds("immigration_stacked.rds")
immigration <-
  immigration_stacked %>%
  filter(Z_brader_pos_neg != "control") %>%
  filter(!is.na(Y_s),!is.na(Z)) %>%
  summarize(
    mt_n = sum(sample == "mt"),
    gfk_n = sum(sample == "gfk"),
    original_n = sum(sample == "original")
  )

system_threat_stacked <- read_rds("system_threat_stacked.rds")
system_threat <-
  system_threat_stacked %>%
  filter(!is.na(Y_s),!is.na(Z)) %>%
  summarize(
    mt_n = sum(sample == "mt"),
    gfk_n = sum(sample == "gfk"),
    original_n = sum(sample == "original")
  )

expert_economists_stacked_long <- read_rds("expert_economists_stacked_long.rds")
expert_economists <-
  expert_economists_stacked_long %>%
  filter(!is.na(Y_s),!is.na(Z)) %>%
  group_by(anon_ID) %>%
  summarize(sample = unique(sample)) %>%
  summarize(
    mt_n = sum(sample == "mt"),
    gfk_n = sum(sample == "gfk"),
    original_n = sum(sample == "original")
  )

mental_illness_stacked <- read_rds("mental_illness_stacked.rds")
mental_illness <-
  mental_illness_stacked %>%
  filter(!is.na(Y_s),!is.na(Z)) %>%
  summarize(
    mt_n = sum(sample == "mt"),
    gfk_n = sum(sample == "gfk"),
    original_n = sum(sample == "original")
  )

death_penalty_stacked <- read_rds("death_penalty_stacked.rds")
death_penalty <-
  death_penalty_stacked %>%
  filter(Z_CP_3 != "control") %>%
  filter(!is.na(Y_s),!is.na(Z)) %>%
  summarize(
    mt_n = sum(sample == "mt"),
    gfk_n = sum(sample == "gfk"),
    original_n = sum(sample == "original")
  )


aec_ns <-
  bind_rows(
    superordinate_id = superordinate_id,
    patriot_act = patriot_act,
    elite_endorsements = elite_endorsements,
    free_trade = free_trade,
    frame_breadth = frame_breadth,
    polarization = polarization,
    immigration = immigration,
    system_threat = system_threat,
    expert_economists = expert_economists,
    mental_illness = mental_illness,
    death_penalty = death_penalty,
    .id = "study"
  )

# Mullinix et al ----------------------------------------------------------

berganS20_stacked <- read_rds("berganS20_stacked.rds")
brandtS1_stacked <- read_rds("brandtS1_stacked.rds")
caprarielloS2_stacked <- read_rds("caprarielloS2_stacked.rds")
converseS16_stacked <- read_rds("converseS16_stacked.rds")
dennyS17_stacked <- read_rds("dennyS17_stacked.rds")
flavinS4_stacked <- read_rds("flavinS4_stacked.rds")
gashS5_stacked <- read_rds("gashS5_stacked.rds")
jacobsenS7_stacked <- read_rds("jacobsenS7_stacked.rds")
melloS6_stacked <- read_rds("melloS6_stacked.rds")
parmerS15_stacked <- read_rds("parmerS15_stacked.rds")
piazzaS8_stacked <- read_rds("piazzaS8_stacked.rds")
pedullaS18_stacked <- read_rds("pedullaS18_stacked.rds")
shaferS9_stacked <- read_rds("shaferS9_stacked.rds")
thompsonS10_stacked <- read_rds("thompsonS10_stacked.rds")
turagaS11_stacked <- read_rds("turagaS11_stacked.rds")
wallaceS12_stacked <- read_rds("wallaceS12_stacked.rds")

# results -----------------------------------------------------------------

mullinix_studies <-
  bind_rows(
    berganS20 = berganS20_stacked,
    brandtS1 = brandtS1_stacked,
    caprarielloS2 = caprarielloS2_stacked,
    converseS16 = converseS16_stacked,
    dennyS17 = dennyS17_stacked,
    flavinS4 = flavinS4_stacked,
    gashS5 = gashS5_stacked,
    jacobsenS7 = jacobsenS7_stacked,
    melloS6 = melloS6_stacked,
    parmerS15 = parmerS15_stacked,
    pedullaS18 = pedullaS18_stacked,
    piazzaS8 = piazzaS8_stacked,
    shaferS9 = shaferS9_stacked,
    thompsonS10 = thompsonS10_stacked,
    turagaS11 = turagaS11_stacked,
    wallaceS12 = wallaceS12_stacked,
    .id = "study"
  )


mullinix_ns <- 
  mullinix_studies %>%
  group_by(study) %>%
  summarize(
    mt_n = sum(sample == "mt"),
    original_n = sum(sample == "gfk")
  )

# bind and merge ----------------------------------------------------------

study_labels_df <- read_rds("CLM_study_labels_df.rds")

study_ns_df <-
  bind_rows(aec_ns, mullinix_ns) %>%
  left_join(study_labels_df)

write_rds(study_ns_df, "CLM_study_ns_df.rds")

